//
//  WBEmoticonKeyBoard.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/14.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit
/*
 
    自定义表情键盘视图
 
    1. 表情视图-> UICollectionView
    2. 页数指示器 -> UIPageControl
    3. toolbar -> UIstackView
 */

class WBEmoticonKeyBoard: UIView {
    
    // MARK: --加载控件
    fileprivate lazy var toolBar: WBEmoticonToolBar = {
        
        let toolBar = WBEmoticonToolBar()
        toolBar.delegate = self
        
        return toolBar
    }()
    // MARK: --加载表情的视图
    fileprivate lazy var emoticonCollectionView:UICollectionView = {
       
        let rect = CGRect(origin: CGPoint.zero, size: CGSize(width: ScreenWidth, height: self.frame.size.height - 35))
        
        let flowLayout = UICollectionViewFlowLayout()
        
        let collectionView:UICollectionView = UICollectionView(frame: rect, collectionViewLayout: flowLayout)
        
        flowLayout.itemSize = collectionView.frame.size
        
        flowLayout.minimumLineSpacing = 0
        
        flowLayout.minimumInteritemSpacing = 0
        
        flowLayout.scrollDirection = .horizontal
        
        collectionView.isPagingEnabled = true
        
        collectionView.bounces = false
        
        collectionView.backgroundColor = self.backgroundColor
        
        collectionView.dataSource = self
        
        collectionView.delegate = self
        
        collectionView.showsVerticalScrollIndicator = false
        
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.register(WBEmoticonKeyBoardCell.self, forCellWithReuseIdentifier: EmoticonKeyBoardCell_id)
       
        return collectionView
    }()
    
    fileprivate lazy var pageControl: UIPageControl = {
        
        let pageCtr = UIPageControl()
        
        pageCtr.setValue(UIImage(named: "compose_keyboard_dot_selected")!, forKey: "_currentPageImage")
        
        pageCtr.setValue(UIImage(named: "compose_keyboard_dot_normal")!, forKey: "_pageImage")
        
        pageCtr.hidesForSinglePage = true
        
        return pageCtr
    }()

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        // 添加控件
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        
        emoticonCollectionView.scrollToItem(at:IndexPath(item: 0, section: 1), at: [], animated: false)
        
        pageControl.snp.makeConstraints { (make) in
            
            make.bottom.equalTo(emoticonCollectionView)
            
            make.centerX.equalTo(emoticonCollectionView)
            
            make.height.equalTo(10)
        }
        
        
        toolBar.snp.makeConstraints { (make) in
            
            make.bottom.left.right.equalTo(self)
            make.height.equalTo(35)
        }
        
    }
    
    func setPageVontrolData(indexPath: IndexPath) {
        
        pageControl.numberOfPages = WBEmoticonTools.sharedTools.allEmoticonArray[indexPath.section].count
        
        pageControl.currentPage = indexPath.item
        
    }
    
    func reloadRecentData() {
        
        let indexPath:IndexPath = IndexPath(item: 0, section: 0)
        
        emoticonCollectionView.reloadItems(at: [indexPath])
    }
    
}
// MARK: --WBEmoticonToolBarDelegate的代理
extension WBEmoticonKeyBoard:WBEmoticonToolBarDelegate {
    
    func emoticonToolBarButtonDidSelected(type: WBEmoticonType) {
        
        let indexPath: IndexPath
        
        switch type {
        case .recent:
            print("最近")
            indexPath = IndexPath(item: 0, section: 0)
        case .normal:
            print("默认")
            indexPath = IndexPath(item: 0, section: 1)
        case .emoji:
            print("Emoji")
            indexPath = IndexPath(item: 0, section: 2)
        case .lxh:
            print("浪小花")
            indexPath = IndexPath(item: 0, section: 3)
       
        }
        
        self.emoticonCollectionView.scrollToItem(at: indexPath, at: [], animated: false)
    }
}

// MARK: --数据源方法,代理方法
extension WBEmoticonKeyBoard:UICollectionViewDataSource,UICollectionViewDelegate {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        
        return WBEmoticonTools.sharedTools.allEmoticonArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WBEmoticonTools.sharedTools.allEmoticonArray[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EmoticonKeyBoardCell_id, for: indexPath) as! WBEmoticonKeyBoardCell
        
        cell.indexPath = indexPath
        
        cell.emoticonArray = WBEmoticonTools.sharedTools.allEmoticonArray[indexPath.section][indexPath.item]
        
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let contentCenterX = scrollView.contentOffset.x + scrollView.frame.size.width * 0.5
        let contentCenterY = scrollView.contentOffset.y + scrollView.frame.size.height * 0.5
        let contentCenter = CGPoint(x: contentCenterX, y: contentCenterY)
                                    // 根据中心点获取cell对应的indexPath
        if let indexPath = emoticonCollectionView.indexPathForItem(at: contentCenter) {
            
            let section = indexPath.section
            
            toolBar.selectedButtonWithSection(section: section)
            
            setPageVontrolData(indexPath: indexPath)
            
        }
        
        
    }
}
